﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace FisheditIniEdit
{
    public static class inilist
    {
        public struct R_modify
        {
            public string typeName;
            public List<R_dataType> ListR;
        }
        /*
         ; 数组个数，
         * 数组结构成员个数，
         * 字节数，
         * 是否字串，
         * 是否名称，
         * 引用数据类型,
         * 名字，
         * 说明
      ; 间隔使用空格，一定要注意字符串中间不能有空格，而且空格只能有一个，等号后面不能有空格。
         */
        public struct R_dataType
        {
            public string dataNum;
            public string dataStructNum;
            public string byteNum;
            public string isString;
            public string isName;
            public string dataType;
            public string nameStr;
            public string DescriptionStr;
        }
        public static R_dataType RdatatypeInital()
        {
            //rtmp.byteNum = "0";
            R_dataType Rtmp = new R_dataType();
            Rtmp.dataNum = "1";
            Rtmp.dataStructNum = "1";
            Rtmp.byteNum = "2";
            Rtmp.isString = "0";
            Rtmp.isName = "0";
            Rtmp.dataType = "-1";
            Rtmp.nameStr = "新建项目";
            Rtmp.DescriptionStr = "双击输入详细内容";
            return Rtmp;
        }
        public static List<R_modify> RiniInital()
        {
            List<R_modify> rini = new List<R_modify>();
            R_modify rmodtmp = new R_modify();
            rmodtmp.typeName = "新建类型";
            rmodtmp.ListR = new List<R_dataType>();
            R_dataType rdatatmp = RdatatypeInital();
            rmodtmp.ListR.Add(rdatatmp);
            rini.Add(rmodtmp);
            return rini;
        }
        public static int saveiniList(List<R_modify> R_ini, string inifilename)
        {
            try
            {
                sfeadd.IniOperation sfeini = new sfeadd.IniOperation(inifilename);
                int TypeNumber = R_ini.Count;//sfeini.ReadIniInt("R_Modify", "TypeNumber");
                sfeini.PutIniString("R_Modify", "TypeNumber", TypeNumber.ToString());
                for (int num = 0; num < TypeNumber; num++)
                {
                    sfeini.PutIniString("R_Modify", "TypeName" + num.ToString(), R_ini[num].typeName.ToString());
                    int TypeDataItem = R_ini[num].ListR.Count;//sfeini.ReadIniInt("R_Modify", "TypeDataItem" + num.ToString());
                    sfeini.PutIniString("R_Modify", "TypeDataItem" + num.ToString(), TypeDataItem.ToString());
                    try
                    {
                        for (int i = 0; i < TypeDataItem; i++)
                        {
                            string tmpstr = string.Format("{0} {1} {2} {3} {4} {5} {6} {7}", R_ini[num].ListR[i].dataNum, R_ini[num].ListR[i].dataStructNum, R_ini[num].ListR[i].byteNum, R_ini[num].ListR[i].isString, R_ini[num].ListR[i].isName, R_ini[num].ListR[i].dataType, R_ini[num].ListR[i].nameStr, R_ini[num].ListR[i].DescriptionStr);
                            sfeini.PutIniString("R_Modify", string.Format("data({0},{1})", num, i), tmpstr);
                        }
                    }
                    catch
                    { }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public static List<R_modify> ini2list(string inifilename)
        {
            try
            {
                sfeadd.IniOperation sfeini = new sfeadd.IniOperation(inifilename);
                int TypeNumber = sfeini.ReadIniInt("R_Modify", "TypeNumber");
                List<R_modify> R_ini = new List<R_modify>();
                for (int num = 0; num < TypeNumber; num++)
                {
                    R_modify Rmtmp = new R_modify();
                    Rmtmp.typeName = sfeini.ReadIniString("R_Modify", "TypeName" + num.ToString());
                    Rmtmp.ListR = new List<R_dataType>();
                    int TypeDataItem = sfeini.ReadIniInt("R_Modify", "TypeDataItem" + num.ToString());
                    for (int i = 0; i < TypeDataItem; i++)
                    {
                        string tmpstr = sfeini.ReadIniString("R_Modify", string.Format("data({0},{1})", num, i));
                        string[] splitstr = tmpstr.Split(' ');
                        R_dataType Rtmp = new R_dataType();
                        //煞笔般的分割导入
                        Rtmp.dataNum = splitstr[0].ToString();
                        Rtmp.dataStructNum = splitstr[1].ToString();
                        Rtmp.byteNum = splitstr[2].ToString();
                        Rtmp.isString = splitstr[3].ToString();
                        Rtmp.isName = splitstr[4].ToString();
                        Rtmp.dataType = splitstr[5].ToString();
                        Rtmp.nameStr = splitstr[6].ToString();
                        Rtmp.DescriptionStr = splitstr[7].ToString();
                        Rmtmp.ListR.Add(Rtmp);
                    }
                    R_ini.Add(Rmtmp);
                }
                return R_ini;
            }
            catch(Exception ee)
            {
                MessageBox.Show("读取ini出错\n不是相同类型文件或者存档损坏\nErrorNum:0x001\n"+ee.Message.ToString());
                return null;
            }
        }
    }
}
